import pygame, math, random
from Controls import *
from Constants import *
# import Spawn, Particle


'''

notes/bugs/to-do


'''




class Game(object):
	'''The game object.'''
	def __init__(self, size, loc, sound):
		'''Initializes the game.'''
		#necessary data
		self.size = size
		self.loc = loc
		self.camsurf = pygame.Surface(size)
		self.camdisp = (0,0)
		self.sound = sound

		#game states
		self.active = False
		self.endgame = False
		self.wingame = False
		self.pause = False
		self.reset = False
		self.leveldata = []

		#control variables
		self.mouse1 = (0,0)
		self.prevmouse = (0,0)

		#gameplay variables

		#game objects

		#object lists
		# self.spawns = []
		# self.bg_spawns = []
		# self.fg_spawns = []

		#hud
		#self.hud = Hud.Hud()

	def newgame(self):
		'''Prepares a new game.'''
		self.active = False
		self.endgame = False
		self.wingame = False
		self.pause = False
		self.reset = False

	def newlevel(self, leveldata=""):
		'''Interprets a loaded level file's data into a new level. Prepares objects for a new level.'''
		#reset game states
		self.leveldata = leveldata
		self.active = True
		self.endgame = False
		self.wingame = False
		self.pause = False
		self.reset = False

		#override

	def resetlevel(self):
		'''Resets the current level without reloading it.'''
		#save check before?
		self.setpause(False)
		self.newlevel(self.leveldata)

	def quitgame(self):
		'''Quit out of the current game.'''
		#may include saving data (check/auto before/during?)
		self.setpause(False)
		self.active = False
		self.reset = False
		self.endgame = True

	def forcequit(self):
		'''Is called upon the program exiting. Can save any data here or halt the program from quitting by returning False.'''
		return True

	def setpause(self, p):
		'''Pauses or unpauses the game.'''
		self.pause = p
		self.active = not p
		# if p:
			
		# else:
			

	def convertmouse(self, m1):
		'''Returns the relative location of a position within the game.'''
		return (m1[0] - self.loc[0], m1[1] - self.loc[1])

	def unconvertmouse(self):
		'''Returns the absolute location of game's mouse on the window containing the game.'''
		return (self.mouse1[0] + self.loc[0], self.mouse1[1] + self.loc[1])

	def setmouse(self, m1):
		'''Sets the game's location and states for the mouse.'''
		self.mouse1 = self.convertmouse(m1)

	def changemouse(self, m1):
		'''Changes the mouse's location on screen. Returns whether the mouse has moved.'''
		if CHANGEMOUSE:
			return self.mouse1 != self.convertmouse(m1)
		return False

	def handleevent(self, event):
		'''Interprets the given event.'''
		if event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 1:
				controls["lmb"] = True
				new_events["lmb"] = True
			elif event.button == 3:
				controls["rmb"] = True
				new_events["rmb"] = True
		elif event.type == pygame.MOUSEBUTTONUP:
			if event.button == 1:
				controls["lmb"] = False
				new_events["lmb"] = True
			elif event.button == 3:
				controls["rmb"] = False
				new_events["rmb"] = True
		if event.type == pygame.KEYDOWN:
			if event.key not in controls:
				return
			controls[event.key] = True
			new_events[event.key] = True
			if event.key == k_quit or event.key == k_pause:
				self.setpause(True)
			# elif event.key == 
		elif event.type == pygame.KEYUP:
			if event.key not in controls:
				return
			controls[event.key] = False
			new_events[event.key] = True
			# if event.key == 

	def update(self, delta):
		'''Updates all game objects.'''
		#override
		pass

	def draw(self, surf):
		'''Draws all game objects.'''
		self.camsurf.blit(black, self.camdisp)

		# for s in self.stuffs:
			# s.draw(self.camsurf, self.camdisp)

		surf.blit(self.camsurf, self.loc)
